package com.muchenxinxi.androidbaseapp.base.mvc;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.muchenxinxi.androidbaseapp.utils.AppActivityManager;
import com.muchenxinxi.androidbaseapp.utils.InjectContentViewUtils;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ┏--------------------
 * │        /) /)
 * │     ฅ(• ﻌ •)ฅ
 * │  Created by HaoKing on 2017/2/23.
 * │  www.muchenxinxi.com
 * │  www.nextdever.com
 * │  QQ:773693552
 * │  QQ群: Geek Chat 344386592
 * ┗--------------------
 */

public abstract class BaseMVCActivity extends AppCompatActivity implements View.OnClickListener {

    private Unbinder mUnbinder;

//    public abstract int getContentViewLayoutId();

    public abstract void initView();

    public abstract void initData();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setLollipopStatusBar();
        //TODO ↓下面两种方式在开始开发前请务必确定好！
        //调用抽象方法来返回布局ID
//        setContentView(getContentViewLayoutId());
        //使用注解+反射来设置布局
        InjectContentViewUtils.inject(this);

        //ButterKnife注解绑定
        mUnbinder = ButterKnife.bind(this);
        //将该Activity加入管理列表
        AppActivityManager.getInstance().pushActivity(this);

        initView();
        initData();
    }

    /**
     * 适配萝莉炮全透明状态栏
     */
    private void setLollipopStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            // 当皮肤为深色的时候再进行调整
            // window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            // | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }

    public void setStatusBarColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.setStatusBarColor(color);
        }
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 通过Action跳转界面
     **/
    public void startActivity(String action) {
        startActivity(action, null);
    }

    /**
     * 含有Bundle通过Action跳转界面
     **/
    public void startActivity(String action, Bundle bundle) {
        Intent intent = new Intent();
        intent.setAction(action);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle, int requestCode) {
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //ButterKnife解除绑定，注意解绑后该Activity的所有View均被置为null
        mUnbinder.unbind();
        //将该Activity从管理列表移除
        AppActivityManager.getInstance().popActivity(this);
    }

}
